#!/usr/bin/python
# -*- coding:utf-8 -*-
import maya.cmds as mc
import maya.mel as mel
import maya.OpenMaya as OpenMaya

from mayaPrint import MayaPrint
from attributes import AttributeManage
from strings import Strings
import os,re

class UIAbout( MayaPrint ,AttributeManage ,Strings ):
    
    def uiReadAndSetOptionVar(self,*opv,**flags):
        """zhaochunhai optionVar for funtion"""
        defineFlags = {"e":(True,bool),
                        "q":(False,bool,"",str,u"",unicode),
                        "buttonType":("",str),
                        "varOption":("",str),
                        "buttonName":("",str),
                        "default":(0,int,"",str,[],list,0.0,float)
                        }
        flagDirect = self.funtionFlag(defineFlags,**flags)
        #read from flagDirect
        e = flagDirect[ "e" ]
        q = flagDirect[ "q" ]
        
        buttonType = flagDirect[ "buttonType" ]
        buttonName = flagDirect[ "buttonName" ]
        varOption = flagDirect[ "varOption" ]
        
        if len(opv)==1:
            varOption = opv[0]
        elif varOption=="":
            varOption = "%s_opv"%(buttonName)
        defaultValue = flagDirect[ "default" ]
        
        #set option value default
        if mc.optionVar( exists=varOption )==False:
            if type(defaultValue)==int:
                mc.optionVar( iv=(varOption, defaultValue) )
            elif type(defaultValue)==str or type(defaultValue)==unicode:
                mc.optionVar( sv=(varOption, defaultValue) )
            elif type(defaultValue)==float:
                mc.optionVar( fv=(varOption, defaultValue) )
            return defaultValue
        #query
        if q==True and mc.optionVar( exists=varOption ):
                return mc.optionVar( q = varOption)
        #Edit optionVar
        if buttonType =="radioButtonGrp":
            if e==True:
                selIndex = mc.radioButtonGrp(buttonName,q=True,select=True)
                mc.optionVar( iv=(varOption, selIndex) )
                return varOption,selIndex
        elif buttonType =="checkBox":
            if e==True:
                v = mc.checkBox(buttonName,q=True,v=True)
                mc.optionVar( iv=(varOption, v) )
                return varOption,v
        elif buttonType =="checkBoxGrp":
            numCheckBoxes = mc.checkBoxGrp(buttonName,q=True,numberOfCheckBoxes=True)
            if numCheckBoxes==1 and e==True:
                v = mc.checkBox(buttonName,q=True,v1=True)
                mc.optionVar( iv=(varOption, v) )
                return varOption,v
            elif  numCheckBoxes==2 and e==True:
                v = mc.checkBox(buttonName,q=True,va2=True)
                mc.optionVar( iv=(varOption, v) )
                return varOption,v
            elif  numCheckBoxes==3 and e==True:
                v = mc.checkBox(buttonName,q=True,va3=True)
                mc.optionVar( iv=(varOption, v) )
                return varOption,v
            elif  numCheckBoxes==4 and e==True:
                v = mc.checkBox(buttonName,q=True,va4=True)
                mc.optionVar( iv=(varOption, v) )
                return varOption,v
        elif buttonType =="textFieldGrp":
            if e==True:
                text = mc.textFieldGrp(buttonName,q=True,text=True)
                mc.optionVar( sv=(varOption, text) )
                return varOption,text
        elif buttonType=="textFieldButtonGrp":
            if e==True:
                text = mc.textFieldButtonGrp(buttonName,q=True,text=True)
                mc.optionVar( sv=(varOption, text) )
                return varOption,text
        elif buttonType=="optionMenuGrp":
            if e==True:
                slVal = mc.optionMenuGrp(buttonName,q=True,sl=True)
                mc.optionVar( iv=(varOption, slVal) )
                return varOption,slVal
        elif buttonType=="textScrollList":
            if e==True:
                slVal = mc.textScrollList(buttonName,q=True,sii=True)
                if slVal!=None:
                    slVal = slVal[0]
                    mc.optionVar( iv=(varOption, slVal) )
                    return varOption,slVal
        
            
            
    def winMenuBar(self,resetCmd,doc=None):
        "menu bar 's item Edit Help"
        
        mc.menu(label="Edit",tearOff=False,allowOptionBoxes=True)
        mc.menuItem( label="Save Seting" ,tearOff=False )
        mc.menuItem( label="Reset Seting" ,tearOff=False ,c=resetCmd)
        mc.menuItem( divider=True)
        mc.radioMenuItemCollection()
        mc.menuItem( label="As Tool" ,rb=True,en=0)
        mc.menuItem( label="As Action" ,rb=True,en=0)
        mc.setParent(menu=True)
        mc.menu(label="Help",tearOff=False,allowOptionBoxes=True)
        helpCmd = "import %s as ui\nui.UIAboutCls.openHelpDoc('%s')"%(__name__,doc)
        mc.menuItem(label="Help On This Tool ...",tearOff=False,c=helpCmd)
        mc.setParent(menu=True)
        
    def openHelpDoc(self,doc):
        docRootPath1 = "\\\\storage1.of3d.com\\file\\dragon\\programming\\publish\\DOC\\zhaochunhai\\WebHelp\\"
        docFile1 = "%s%s"%( docRootPath1,doc )
        
        docRootPath2 = "Z:\\doc\\WebHelp\\"
        docFile2 = "%s%s"%( docRootPath2,doc )
        
        if os.path.exists(docFile2):
            os.system( 'explorer "%s"'%( docFile2 ) )
        elif os.path.exists(docFile1):
            os.system( 'explorer "%s"'%( docFile1 ) )
        else:
            self.mayaPrint('Email:zhaochunhai@of3d.com or 51962215@qq.com')
        
    def winMasterLayout(self,resetCmd,applyCmd,closeWindow,label1="Reset Tool",label2="Apply",label3="Close"):
        "every window bottom three button,return a columnLayout for storage other ui element"
        
        closeCmd = "import %s as ui\nui.UIAboutCls.closeWindow('%s')"%(__name__,closeWindow)
        
        thisRoot=mc.formLayout(numberOfDivisions=3)
        aFramLayout = mc.frameLayout( lv=0, borderStyle='etchedOut' )
        res = mc.columnLayout(adj=True)
        mc.setParent(thisRoot)
        #[Reset Tool] [Apply]  [Close]
        reb = mc.button(label=label1,h=27,c=resetCmd)
        apb = mc.button(label=label2,h=27,c=applyCmd)
        clb = mc.button(label=label3,h=27,c=closeCmd)
        mc.formLayout(thisRoot,edit=True,
        ap=[(aFramLayout,"left",2,0),(aFramLayout,"top",0,0),(aFramLayout,"right",2,3),
            (reb,"left",5,0),(reb,"bottom",5,3),(reb,"right",0,1),
            (apb,"left",0,1),(apb,"bottom",5,3),(apb,"right",0,2),
            (clb,"left",0,2),(clb,"bottom",5,3),(clb,"right",5,3)],
        ac=[(aFramLayout,"bottom",1,apb)]
        )
        mc.setParent(res)
        return res
    def winInfo(self):
        mc.frameLayout(lv=0,bs="etchedOut")
        mc.text(label="(C)Original Force-TD | ChunHai Zhao",h=22,en=0,al="center")
        
    def confirmDialog(self,msg = 'There is a check warning,will you?',bList=['Ignore','StopChek']):
        reslut = mc.confirmDialog( title='Warning...', message=msg, button=bList,dismissString='StopChek' )
        return reslut
        
    def loadSelectedIntoButtonGrp(self,buttonGrp):
        objs = mc.ls(sl=True)
        if (len(objs) > 0):
            mc.textFieldButtonGrp(buttonGrp,e=True,tx=self.changListToStr(objs) )
    def loadSelectedAtrIntoButtonGrp(self,buttonGrp):
        attrName = self.getSelectedAttr()
        if attrName!="":
            mc.textFieldButtonGrp( buttonGrp,e=True,tx=attrName )
        
    def closeWindow(self,winName):
        if mc.window(winName, exists=True):
            mc.deleteUI(winName, window=True)

UIAboutCls = UIAbout()